package com.inspur.icity.bus.base.core.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/15 0015.
 */
public class FileUtils {

    /**
     * 导出EXCEL
     *
     * @param workbook
     * @param codedFileName
     */
    public static void exportFile(HSSFWorkbook workbook, String codedFileName, HttpServletResponse response) {
        OutputStream fOut = null;
        try {
            // 生成提示信息，
            response.setContentType("application/vnd.ms-excel");
            // 进行转码，使其支持中文文件名
            codedFileName = java.net.URLEncoder.encode(
                    codedFileName + DateUtil.TimeToForDay(DateUtil.getSystemTime()), "UTF-8");
            response.setHeader("content-disposition", "attachment;filename="
                    + codedFileName + ".xls");

            fOut = response.getOutputStream();
            workbook.write(fOut);
        } catch (UnsupportedEncodingException e1) {
        } catch (Exception e) {
        } finally {
            try {
                if (fOut != null) {
                    fOut.flush();
                    fOut.close();
                }
                if (workbook != null) {
                    workbook.close();
                }
            } catch (IOException e) {
            }
        }
    }

    /**
     * 构造EXCEL内容
     * @param rowNo 行号
     * @param cellMap 列号：内容
     * @return
     */
    public static HSSFWorkbook buildExcelContent(int rowNo , Map<Integer,String> cellMap , List<Map<Integer,String>> contentList){
        // 产生工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 产生工作表对象
        HSSFSheet sheet = workbook.createSheet();
        // 创建第rowNo行
        HSSFRow row = sheet.createRow(rowNo-1);

        HSSFCell cell;
        for(Map.Entry entry : cellMap.entrySet()){
            cell = row.createCell((int)entry.getKey()-1);// 创建第2列
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            cell.setCellValue((String)entry.getValue());
        }

        for(Map<Integer,String> map : contentList){

            HSSFRow contentRow = sheet.createRow(rowNo);
            for(Map.Entry entry : map.entrySet()){
                cell = contentRow.createCell((int)entry.getKey()-1);// 创建列
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue((String)entry.getValue());
            }
            rowNo++;
        }
        return workbook;
    }


    /**
     * 上传文件
     * @param file       文件内容
     * @param targetPath 目标路径
     * @return
     */
    public static String uploadFile(MultipartFile file, String targetPath) {
        String sourceFileName = file.getOriginalFilename();
        String name = MD5Util.MD5Encode(sourceFileName) +"."+sourceFileName.split("\\.")[1];
        if(!new File(targetPath).exists()){
            new File(targetPath).mkdirs();
        }
        File tmpFile = new File(targetPath + name);
        try {
            file.transferTo(tmpFile);
        } catch (Exception e) {
            return null;
        }
        return name;
    }
}
